﻿using System.Net;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using JTTD.Common.Attribute;
using JTTD.Domain;
using JTTD.Domain.Dtos;
using JTTD.Domain.Dtos.Activity.Response;
using JTTD.Domain.Dtos.Coupon.Request;
using JTTD.Domain.Dtos.Coupon.Response;
using JTTD.Domain.IService;
using JTTD.Extensions;
using Microsoft.AspNetCore.Mvc;

namespace JTTD.Controllers
{
    /// <summary>
    /// 商城优惠券
    /// </summary>
    [Route("api/coupons")]
    [ApiController]
    public class CouponController : BaseController
    {
        private readonly ICouponService _couponService;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="couponService"></param>
        public CouponController(ICouponService couponService)
        {
            _couponService = couponService;
        }

        /// <summary>
        /// 分页获取所有停车优惠券(管理员后台)
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("allCoupons")]
        [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(ApiResult<PageResult<CouponInfoResponseDto>>))]
        [UserRoot(UserType = Common.Enum.UserTypeEnum.Admin)]
        public async Task<IActionResult> GetAllCouponPageList([FromQuery]CouponSearchRequestDto dto)
        {
            var result = await _couponService.GetAllCouponPageList(dto);
            return Ok(result);
        }

        /// <summary>
        /// 分页获取指定用户能使用的商城优惠券(微信公众号)
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("")]
        [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(ApiResult<PageResult<CouponInfoResponseDto>>))]
        [UserRoot(UserType=Common.Enum.UserTypeEnum.Ordinary)]
        public async Task<IActionResult> GetCouponPageList([FromQuery]BaseSearch dto)
        {
            var result = await _couponService.GetCouponPageList(UserId, dto);
            return Ok(result);
        }

        /// <summary>
        /// 兑换商城优惠券(微信公众号)
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("coupon")]
        [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(ApiResult<bool>))]
        [UserRoot(UserType = Common.Enum.UserTypeEnum.Ordinary)]
        public async Task<IActionResult> ExchangeCoupon([FromBody]ExchangeCouponRequestDto dto)
        {
            var result = await _couponService.ExchangeCoupon(UserId,dto.Amount);
            return Ok(result);
        }

        /// <summary>
        /// 赠送优惠券(管理员后台)
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("coupon/send")]
        [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(ApiResult<bool>))]
        [UserRoot(UserType = Common.Enum.UserTypeEnum.Admin)]
        public async Task<IActionResult> SendCoupon([FromBody]SendCouponRequestDto dto)
        {
            var result = await _couponService.ExchangeCoupon(dto.UserId, dto.Amount);
            return Ok(result);
        }

        /// <summary>
        /// 商城优惠券核销(管理后台)
        /// </summary>
        /// <param name="couponNumber">优惠券号</param>
        /// <returns></returns>
        [HttpPut]
        [Route("{couponNumber}")]
        [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(ApiResult<ActivityInfoResponseDto>))]
        [UserRoot(UserType = Common.Enum.UserTypeEnum.Admin)]
        public async Task<IActionResult> UseCoupon([FromRoute]string couponNumber)
        {
            var result = await _couponService.ChangeUseState(couponNumber,true);
            return Ok(result);
        }

        /// <summary>
        /// 商城优惠券撤销核销(管理后台)
        /// </summary>
        /// <param name="couponNumber">优惠券号</param>
        /// <returns></returns>
        [HttpPut]
        [Route("revokeCoupon/{couponNumber}")]
        [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(ApiResult<ActivityInfoResponseDto>))]
        [UserRoot(UserType = Common.Enum.UserTypeEnum.Admin)]
        public async Task<IActionResult> RevokeCoupon([FromRoute]string couponNumber)
        {
            var result = await _couponService.ChangeUseState(couponNumber, false,this.UserId);
            return Ok(result);
        }

        /// <summary>
        /// 撤销商城优惠券（删除商城优惠券）
        /// </summary>
        /// <param name="couponNumber"></param>
        /// <returns></returns>
        [HttpDelete]
        [Route("{couponNumber}")]
        [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(ApiResult<bool>))]
        [UserRoot(UserType = Common.Enum.UserTypeEnum.Admin)]
        public async Task<IActionResult> DeleteCoupon([FromRoute]string couponNumber)
        {
            var result = await _couponService.DeleteCoupon(couponNumber,this.UserId);
            return Ok(result);
        }

    }
}